import 'dart:async';
import 'package:flutter/services.dart';
import '../../utils/platform.dart';

/// 探鸽 - token文件
MethodChannel _methodChannel = const MethodChannel('com.tange.ai/rapid_kit_example/method.channel/notification');
EventChannel _eventChannel = const EventChannel('com.tange.ai/rapid_kit_example/event.channel/notification');
StreamSubscription? _eventSubscription;
typedef NotificationEventListener = void Function(String data);

class NotificationChannel {
  static void startListening(NotificationEventListener listener) {
    _eventSubscription = _eventChannel.receiveBroadcastStream().listen((event) {
      // Once receiced the event, we can directly "getPendingNotification"
      // and then dispatch the notification to the listener.
      getPendingNotification().then((value) {
        if (value.isNotEmpty) {
          listener(value);
        }
      });
    });
  }

  static void stopListening() {
    _eventSubscription?.cancel();
  }

  static Future<String> getPendingNotification() async {
    final String? result = await _methodChannel.invokeMethod<String>('getPendingNotification');
    if (result != null) {
      return result;
    } else {
      throw Exception('Expected String but got null');
    }
  }

  static Future<String> getPushServiceToken() async {
    final String? result = await _methodChannel.invokeMethod<String>('getPushServiceToken');
    if (result != null) {
      return result;
    } else {
      throw Exception('Expected String but got null');
    }
  }

  static Future<bool> isNotificationEnabled() async {
    // Notice: we only provide sample on OpenHarmonyOS.
    if (!PlatformUtil.ohos()) {
      return true;
    }

    final bool? result = await _methodChannel.invokeMethod<bool>('isNotificationEnabled');
    if (result != null) {
      return result;
    } else {
      throw Exception('Expected bool but got null');
    }
  }

  static Future<void> requestNotificationEnabled() async {
    await _methodChannel.invokeMethod<void>('requestNotificationEnabled');
  }
}
